package com.wkhyapp.lm.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceUtils {
    private static PreferenceUtils sInstance;
    private final SharedPreferences mPref;
    private static String NAME = "MEMBER_IM";

    private PreferenceUtils(Context context) {
        mPref = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
    }

    public static synchronized void initializeInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferenceUtils(context);
        }
    }

    public static synchronized PreferenceUtils getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException(PreferenceUtils.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        return sInstance;
    }

    public String getPrefString(String key,
                                final String defaultValue) {
        return mPref.getString(key, defaultValue);
    }


    public void setPrefString(final String key,
                              final String value) {
        mPref.edit().putString(key, value).commit();
    }


    public boolean getPrefBoolean(String key,
                                  final boolean defaultValue) {
        return mPref.getBoolean(key, defaultValue);
    }

    public void setPrefBoolean( final String key,
                                final boolean value) {
        mPref.edit().putBoolean(key, value).commit();
    }


    public  int getPrefInt(final String key,
                           final int defaultValue) {

        return mPref.getInt(key, defaultValue);
    }

    public  void setPrefInt(final String key,
                            final int value) {
        mPref.edit().putInt(key, value).commit();
    }


    public  void setPrefFloat(final String key,
                              final float value) {
        mPref.edit().putFloat(key, value).commit();
    }

    public  float getPrefFloat(final String key,
                               final float defaultValue) {
        return mPref.getFloat(key, defaultValue);
    }


    public  void setSettingLong(final String key,
                                final long value) {

        mPref.edit().putLong(key, value).commit();
    }

    public  long getPrefLong( final String key,
                              final long defaultValue) {

        return mPref.getLong(key, defaultValue);
    }


    ////////////////////////////////


    public static String getPrefString(Context context, String key,
                                       final String defaultValue) {
        return getInstance().getPrefString(key, defaultValue);
    }

    public static void setPrefString(Context context, final String key,
                                     final String value) {
        getInstance().setPrefString(key, value);
    }

    public static boolean getPrefBoolean(Context context, final String key,
                                         final boolean defaultValue) {
        return getInstance().getPrefBoolean(key, defaultValue);
    }

    public static boolean hasKey(Context context, final String key) {
        return PreferenceManager.getDefaultSharedPreferences(context).contains(
                key);
    }

    public static void setPrefBoolean(Context context, final String key,
                                      final boolean value) {
        getInstance().setPrefBoolean(key, value);
    }

    public static void setPrefInt(Context context, final String key,
                                  final int value) {
        getInstance().setPrefInt(key, value);
    }

    public static int getPrefInt(Context context, final String key,
                                 final int defaultValue) {
        return getInstance().getPrefInt(key, defaultValue);
    }

    public static void setPrefFloat(Context context, final String key,
                                    final float value) {
        getInstance().setPrefFloat(key, value);
    }

    public static float getPrefFloat(Context context, final String key,
                                     final float defaultValue) {
        return getInstance().getPrefFloat(key, defaultValue);
    }

    public static void setSettingLong(Context context, final String key,
                                      final long value) {
        getInstance().setSettingLong(key, value);
    }

    public static long getPrefLong(Context context, final String key,
                                   final long defaultValue) {
        return getInstance().getPrefLong(key, defaultValue);
    }

    public static void clearPreference(Context context,
                                       final SharedPreferences p) {
        final SharedPreferences.Editor editor = p.edit();
        editor.clear();
        editor.commit();
    }
}
